const { program } = require("commander");
const createProject = require("./create");
const path = require("path");

const { version } = require("./utils/constants");

const actionsMap = {
  create: {
    description: "create project",
    alias: "cr",
    examples: ["zhu-cli create <template-name>"],
  },
  config: {
    description: "config info",
    alias: "c",
    examples: ["zhu-cli config get <k>", "zhu-cli config set <k> <v>"],
  },
};

// 循环创建命令
Object.keys(actionsMap).forEach((action) => {
  program
    .command(action)
    .alias(actionsMap[action].alias)
    .description(actionsMap[action].description)
    .action(() => {
      if (action === "create") {
        console.log(process.argv, "process.argvprocess.argvprocess.argv");
        createProject(...process.argv.slice(3)); // 调用导入的默认函数
      } else {
        console.log(`Action "${action}" is not implemented yet.`);
      }
    });
});

// 捕获未定义的命令
program
  .command("*")
  .description("command not found")
  .action(() => {
    console.log("command not found");
  });

program.on("--help", () => {
  console.log("Examples");
  Object.keys(actionsMap).forEach((action) => {
    (actionsMap[action].examples || []).forEach((example) => {
      console.log(`${example}`);
    });
  });
});

program.version(version).parse(process.argv);
